package com.cloudcc.mobile.dao.impl;

import java.util.List;

import android.text.TextUtils;

import com.cloudcc.cloudframe.net.RequestListener;
import com.cloudcc.cloudframe.net.async.EventRequest;
import com.cloudcc.cloudframe.net.async.JsonObjectRequest;
import com.cloudcc.mobile.AppConstant;
import com.cloudcc.mobile.dao.BaseEngine;
import com.cloudcc.mobile.dao.MarketActivityEngine;
import com.cloudcc.mobile.entity.market.MarketActivityEntity;
import com.cloudcc.mobile.entity.market.MarketPhoneCheckEntity;
import com.cloudcc.mobile.entity.market.MarketPhoneNumberCheckResult;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketCheckPhoneEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketCheckPhoneNumberEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketEventInfoEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketEventListEvent;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.loopj.android.http.RequestParams;

public class MarketActivityEngineImpl extends BaseEngine implements
		MarketActivityEngine {

	@Override
	public void getMarketList(int page,String key, MarketEventListEvent event) {
		EventRequest<List<MarketActivityEntity>> request = new EventRequest<List<MarketActivityEntity>>(){};
		request.setEvent(event);
		String expressions=TextUtils.isEmpty(key)?"1=1 ORDER BY createdate Desc":"name like '%"+key+"%' ORDER BY createdate Desc";
		queryObjAPi("campaign", "pageQuery", page, AppConstant.PAGE_SIZE, expressions, request);
	}

	@Override
	public void getMarketList(String id, MarketEventInfoEvent event) {
		EventRequest<List<MarketActivityEntity>> request = new EventRequest<List<MarketActivityEntity>>(){};
		request.setEvent(event);
		queryObjAPiWithRoleById("campaign", id, request);
	}

	@Override
	public void checkByPhone(String phoneNubmer, String marketId,
			RequestListener li) {
		RequestParams params = new RequestParams();
		params.put("serviceName", "HMSingIn");
		params.put("telephone", phoneNubmer);
		params.put("campaign", marketId);
		params.put("binding", RunTimeManager.getInstance().getServerBinding());
		JsonObjectRequest request = new JsonObjectRequest();
		request.setRequestListener(li);
		sendPost(UrlManager.getInterfaceUrl(), params,request);
	}

	@Override
	public void checkByQrCode(String memberId, String telephone, String campaign,
			RequestListener li) {
		RequestParams params = new RequestParams();
		params.put("serviceName", "QRSingIn");
		params.put("memberId", memberId);
		params.put("telephone", telephone);
		params.put("campaign", campaign);
		params.put("binding", RunTimeManager.getInstance().getServerBinding());
		JsonObjectRequest request = new JsonObjectRequest();
		request.setRequestListener(li);
		sendPost(UrlManager.getInterfaceUrl(), params, request);
	}

	@Override
	public void valPhoneNumber(String phoneNubmer,String campaign,
			MarketCheckPhoneNumberEvent event) {
		
		EventRequest<List<MarketPhoneNumberCheckResult>> request = new EventRequest<List<MarketPhoneNumberCheckResult>>(){};
		request.setEvent(event);
		
		String expressions="campaign='"+campaign+"' and phone='"+phoneNubmer+"'";
		queryObjAPi("campaignmember", "pageQuery", 1, AppConstant.PAGE_SIZE, expressions, request);
	}

}
